﻿using System;
using System.Collections.Generic;
using Newtonsoft.Json;
using XiaWorld;

namespace WritingBooks
{
    // Token: 0x0200023E RID: 574
    [JsonObject(MemberSerialization = MemberSerialization.OptIn)]
    public class ToilWriting : ToilBase
    {
        // Token: 0x17000121 RID: 289
        // (get) Token: 0x06000BD5 RID: 3029 RVA: 0x0000929A File Offset: 0x0000749A
        public override string ToilAnimation
        {
            get
            {
                return "dazuo";
            }
        }

        public ToilWriting(string eId)
        {
            this.eId = eId;
        }

        // Token: 0x06000BD6 RID: 3030 RVA: 0x0005D814 File Offset: 0x0005BA14
        protected override void OnEnterToil(KStateQUnit unit)
        {
            this.itemID = base.npc.ItemInHand.ID;
            ItemThing itemThing = ThingMgr.Instance.FindThingByID(this.itemID) as ItemThing;
            this.costtime = ((float)itemThing.Rate * 80f + itemThing.LingV / 100f) / (1f + 0.02f * (float)base.npc.PropertyMgr.SkillData.GetSkillLevel(g_emNpcSkillType.Manual) + base.npc.GetProperty("FabaoMake_SpeedAddV"));
            this.costtime = this.costtime / 10;
        }

        // Token: 0x06000BD7 RID: 3031 RVA: 0x00009405 File Offset: 0x00007605
        protected override void OnStepToil(float dt, KStateQUnit unit)
        {
            base.SetProgress(unit.keeptime / this.costtime);
            if (unit.keeptime >= this.costtime)
            {
                this.m_bDid = true;
                unit.IsFinished = true;
            }
        }

        // Token: 0x06000BD8 RID: 3032 RVA: 0x0005D8DC File Offset: 0x0005BADC
        protected override void OnLeaveToil(KStateQUnit unit)
        {
            if (this.m_bDid)
            {
                this.MakeFaBao();
            }
            else
            {
                ItemThing itemThing = ThingMgr.Instance.FindThingByID(this.itemID) as ItemThing;
                itemThing.SubCount(1);
                MsgMgr.Instance.AddMsg(2, base.npc, null, 0, true, false);
                if (base.npc.InBuilding.View != null)
                {
                    base.npc.InBuilding.View.OnWorkFinished(false);
                }
            }
            if (World.Instance.NewBee)
            {
                TeachMgr.Instance.FinishTask(24);
            }
        }

        // Token: 0x06000BD9 RID: 3033 RVA: 0x00009439 File Offset: 0x00007639
        public override string GetDesc()
        {
            return "写书中";
        }

        // Token: 0x06000BDA RID: 3034 RVA: 0x00009440 File Offset: 0x00007640
        public override string GetToilKey()
        {
            return "refining";
        }

        // Token: 0x06000BDB RID: 3035 RVA: 0x00009447 File Offset: 0x00007647
        protected override void OnNpcStoryTrigger()
        {
            NpcMgr.Instance.TriggerNpcStory("Refining", base.npc, null, ThingMgr.Instance.FindThingByID(this.itemID).GetName(), false, false);
        }

        // Token: 0x06000BDC RID: 3036 RVA: 0x0005D980 File Offset: 0x0005BB80
        private void MakeFaBao()
        {
            ItemThing itemThing = ThingMgr.Instance.FindThingByID(this.itemID) as ItemThing;
            float a = 0.6f - 0.4f * (float)itemThing.Rate + 0.1f * (float)base.npc.PropertyMgr.SkillData.GetSkillLevel(g_emNpcSkillType.Manual) + GameDefine.MindState2Refining[base.npc.Needs.GetNeedLevel(g_emNeedType.MindState)] + base.npc.GetProperty("FabaoMake_SuccessRateAddV");
            if (World.RandomRate(a))
            {
                EsotericaData sysEsoterica = EsotericaMgr.Instance.GetSysEsoterica(this.eId);
                ItemThing itemThing2 = ThingMgr.Instance.AddEsotericaItemById(0, this.eId, itemThing.map);
                base.npc.map.DropItem(itemThing2, base.npc.Key, true, true, false, false, 0f);
                float num = npc.PropertyMgr.Practice.GetTreeExpNeed(sysEsoterica.Difficulty) / 4f;
                npc.PropertyMgr.Practice.AddTreeExp(-num);
            }
            else
            {
                base.npc.PropertyMgr.Practice.AddTreeExp(30f, false);
                NpcMgr.Instance.TriggerNpcStory("RefiningFailed", base.npc, null, itemThing.GetName(), false, false);
                MsgMgr.Instance.AddMsg(2, base.npc, null, 0, true, false);
                GameWatch.Instance.BuryingPoint(BuryingPointType.Refining, 0);
            }
            GameWatch.Instance.Achievement.AddCount(g_emAchievementCountKind.Refining_Count, 1f);
            itemThing.SubCount(1);
        }

        // Token: 0x04000C9D RID: 3229
        [JsonProperty]
        private int itemID;

        // Token: 0x04000C9E RID: 3230
        [JsonProperty]
        private float costtime;
        private string eId;
    }
}
